﻿using MXERP.Shared.Security;
using System.Security.Claims;

namespace MXERP.Shared.Extensions
{
    public static class ClaimsIdentityExtensions
    {
        public static long? FindUserId(this ClaimsPrincipal? principal)
        {
            Claim? userIdOrNull = principal?.Claims?.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);
            if (userIdOrNull == null || userIdOrNull.Value.IsNullOrWhiteSpace())
            {
                return null;
            }

            return long.Parse(userIdOrNull.Value);
        }

        public static bool? IsSuperAdmin(this ClaimsPrincipal? principal)
        {
            Claim? isSuperAdminOrNull = principal?.Claims?.FirstOrDefault(c => c.Type == MXERPClaimTypes.IsSuperAdmin);
            if (isSuperAdminOrNull == null || isSuperAdminOrNull.Value.IsNullOrWhiteSpace())
            {
                return null;
            }
            return bool.Parse(isSuperAdminOrNull.Value);
        }

        public static string? FindUserName(this ClaimsPrincipal? principal)
        {
            Claim? userNameOrNull = principal?.Claims?.FirstOrDefault(c => c.Type == ClaimTypes.Name);

            return userNameOrNull?.Value;
        }

    }
}
